from django.db import models
from drf_shop_admin.utils.models import BaseModel

# Create your models here.
class Goods(BaseModel):
    """
    商品信息表
    """
    IS_DEL_CHOICES = (
        (False, '正常'),
        (True, '删除')
    )
    IS_PROMOTE_CHOICES = (
        (False, '正常'),
        (True, '促销')
    )
    STATE_CHOICES = (
        (0, '未审核'),
        (1, '未通过'),
        (2, '审核中'),
        (3, '已审核')
    )
    name = models.CharField('商品名称', max_length=255, blank=False, null=False, unique=True)
    price = models.DecimalField('商品价格', max_digits =10, decimal_places =2, default=0.00)
    number = models.IntegerField('商品数量', default=0)
    weight = models.DecimalField('商品重量', max_digits =10, decimal_places =2, default=0.00)
    introduction = models.TextField('商品详情介绍')
    big_logo = models.CharField('图片logo大图', max_length=128, default='', null=False)
    small_logo = models.CharField('图片logo小图', max_length=128, default='', null=False)
    is_del = models.BooleanField('是否删除', default=False, null=False, choices=IS_DEL_CHOICES)
    delete_time = models.DateTimeField('删除时间', default=None, null=True)
    hot_number = models.IntegerField('热卖数量', default=0)
    is_promote = models.BooleanField('是否促销', default=False, choices=IS_PROMOTE_CHOICES)
    state = models.SmallIntegerField('商品状态', default=0, choices=STATE_CHOICES)
    categories = models.ManyToManyField('Categories', blank=True, related_name='good', verbose_name='商品类别')

    objects = models.Manager()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '商品信息'
        verbose_name_plural = verbose_name
        ordering = ['id']


class Categories(BaseModel):
    """
    商品分类表
    """
    name = models.CharField('分类名称', blank=False, unique=True, max_length=32)
    is_show = models.BooleanField('是否显示', default=True, null=False)
    sort = models.IntegerField('分类排序', null=False, default=1)
    data_flag = models.BooleanField('数据标记', null=False, default=True)
    pid = models.ForeignKey('self', default='', null=True, blank=True, on_delete=models.CASCADE, verbose_name='父级分类')

    objects = models.Manager()

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '商品分类'
        verbose_name_plural = verbose_name
        ordering = ['id']


class Attrs(BaseModel):
    """
    商品属性
    """
    value = models.CharField('商品对应属性的值', null=False, max_length=50)
    add_price = models.DecimalField('该属性需要额外增加的价钱', max_digits=10, decimal_places=2, default=0.00)
    good = models.ForeignKey('Goods', on_delete=models.CASCADE, verbose_name='商品信息')

    objects = models.Manager()

    def __str__(self):
        return self.value

    class Meta:
        verbose_name = '商品属性'
        verbose_name_plural = verbose_name
        ordering = ['id']


class Pics(BaseModel):
    """
    商品相册
    """
    big_pic = models.CharField('相册大图800*800', max_length=128, default='', null=False)
    mid_pic = models.CharField('相册中图350*350', max_length=128, default='', null=False)
    small_pic = models.CharField('相册小图50*50', max_length=128, default='', null=False)
    good = models.ForeignKey('Goods', on_delete=models.CASCADE, verbose_name='商品信息')

    objects = models.Manager()

    def __str__(self):
        return self.good.name

    class Meta:
        verbose_name = '商品相册'
        verbose_name_plural = verbose_name
        ordering = ['id']